#!/bin/bash
# Copyright (C) 2011 all rights reserved 
# GNU GENERAL PUBLIC LICENSE VERSION 3.0
# Author bjarneh@ifi.uio.no
#
# Bash command completion file for cop
#
# Add this file somewhere where it gets sourced.
# If you have sudo power it can be dropped into
# /etc/bash_completion.d/. If not it can be sourced
# by one of your startup scripts (.bashrc .profile ...)

_cop(){

    local cur prev opts cop_long_opts cop_short_opts cop_short_explain
    # long options
    cop_long_opts="--help --version --verbose --timeout --list --dotfile --maxpaths --paths --recursive --strategy --order --plain --cmp --ignore --listcmp --single --seed --dotfmt --dotmax --dotheader --loglevel --ll --logfile --lf --hidden --accurate"

    # short options + explain
    cop_short_explain="-h[--help] -v[--version] -V[--verbose] -t[--timeout] -l[--list] -d[--dotfile] -m[--maxpaths] -P[--paths] -r[--recursive] -s[--strategy] -o[--order] -p[--plain] -c[--cmp] -i[--ignore] -a[--accurate]"
    # short options
    cop_short_opts="-h -v -V -t -l -d -m -P -r -s -o -p -c -i -a"

    COMPREPLY=()
    
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    cur="${COMP_WORDS[COMP_CWORD]}"
    
    if [[ "${cur}" == --* ]]; then
        COMPREPLY=( $(compgen -W "${cop_long_opts}" -- "${cur}") )
        return 0
    fi

    if [[ "${cur}" == -* ]]; then
        COMPREPLY=( $(compgen -W "${cop_short_opts}" -- "${cur}") )
        if [ "${#COMPREPLY[@]}" -gt 1 ]; then
            COMPREPLY=( $(compgen -W "${cop_short_explain}" -- "${cur}") )
        fi
        return 0
    fi
    if [[ "${prev}" == -* ]]; then
        case "${prev}" in
            '-a' | '-arch' | '--arch' | '-arch=' | '--arch=')
                COMPREPLY=( $(compgen -W "arm 386 amd64" -- "${cur}") )
                return 0
                ;;
        esac
    fi
    if [[ "${prev}" == -* ]]; then
        case "${prev}" in
            '-B' | '-B=' | '-backend' | '--backend' | '-backend=' | '--backend=')
                COMPREPLY=( $(compgen -W "gc gccgo express" -- "${cur}") )
                return 0
                ;;
        esac
    fi
}

## fallback on plain
complete -o default -F _cop cop
